<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<link rel="stylesheet" href="css/popup.css" type="text/css">
	<script src="scripts/jquery-1.5.2.min.js" type="text/javascript"></script>	
	<script type="text/javascript">
	var bkP = null;
	
	function findFGSinstance()
	{
	
		var wm = Components.classes['@mozilla.org/appshell/window-mediator;1']
				.getService(Components.interfaces.nsIWindowMediator);
		var browserEnumerator = wm.getEnumerator("navigator:browser");
		
		var found = false;
		
		while (!found && browserEnumerator.hasMoreElements())
		{
			var bE = browserEnumerator.getNext();
			if(bE.FGScheck != undefined)
			{
				found = true;
				break;
			}
		}
		
		if(!found) //  || !bE.FGS.alreadyOpened
		{
			window.location.href = 'about:blank';
		}
		else
		{
			bkP = bE.FGS;
		}
	}
	findFGSinstance();

	function appendDB(gameID, num, name)
	{
		if(gameID == null)
		{
			var stmt = bkP.database.db.createStatement("SELECT COUNT(*) as count FROM "+name);
			
			while (stmt.executeStep())
			{
				$('#totals > tbody').append('<tr><td class="first">'+num+'.</td><td>'+name+'</td><td>'+stmt.row.count+'</td><td class="last"><a href="#" table="'+name+'" class="emptyDB">Empty</a></td></tr>');
			}
			stmt.reset();
		}
		else
		{
			var stmt = bkP.database.db.createStatement("SELECT COUNT(*) as count FROM "+name+"  where gameID = '"+gameID+"'");
			
			while (stmt.executeStep())
			{
				$('#'+gameID+' > tbody').append('<tr><td class="first">'+num+'.</td><td>'+name+'</td><td>'+stmt.row.count+'</td><td class="last"><a href="#" table="'+name+'" gameID="'+gameID+'" class="emptyDB">Empty</a></td></tr>');
			}
			stmt.reset();
		}
	}
	
	
	function listDb()
	{
		$('body').prepend('<table id="totals" style="margin: 0 auto; margin-top: 15px;" width="75%"><caption>Your database usage for all games</caption><thead><tr><th scope="col" class="first"></th><th scope="col">Database name</th><th scope="col">Database count</th><th scope="col" class="last">Actions</th></tr></thead><tbody></tbody></table>');
	
		var stmt = bkP.database.db.createStatement("SELECT * FROM sqlite_master where type = 'table'");
		
		var cnt = 0;
		
		while (stmt.executeStep())
		{
			var re = new RegExp('(^__|sqlite|options)', "i");
			var r = stmt.row;
			if(!re.test(r.name))
			{
				cnt++;
				appendDB(null, cnt, r.name);
			}					
		}
		stmt.reset();		
	
		for(var idd in bkP.options.games)
		{
			$('body').append('<table id="'+idd+'" style="margin: 0 auto; margin-top: 15px;" width="75%"><caption>Your database usage for game: '+bkP.gamesData[idd].name+'</caption><thead><tr><th scope="col" class="first"></th><th scope="col">Database name</th><th scope="col">Database count</th><th scope="col" class="last">Actions</th></tr></thead><tbody></tbody></table>');
			var cnt = 0;
			
			while (stmt.executeStep())
			{
				var re = new RegExp('(^__|sqlite|options)', "i");
				var r = stmt.row;
				if(!re.test(r.name))
				{
					cnt++;
					appendDB(idd, cnt, r.name);
				}					
			}
			stmt.reset();
		}
	}
	
	$(function()
	{
		listDb();
		
		$('.emptyDB').live('click',function()
		{
			var gameID = $(this).attr('gameID');
			var table = $(this).attr('table');
			
			if(typeof(gameID) == "undefined")
			{
				var stmt = bkP.database.db.createStatement('DELETE FROM '+table);
				
				while (stmt.executeStep()){}
				stmt.reset();
				
				bkP.newElements = 0;
				bkP.updateIcon();
				$('body').html('');
				listDb();
			}
			else
			{
				var stmt = bkP.database.db.createStatement("DELETE FROM "+table+" where gameID = '"+gameID+"'");
				
				while (stmt.executeStep()){}
				stmt.reset();
				
				bkP.newElements = 0;
				bkP.updateIcon();
				$('body').html('');
				listDb();
			}
			
		});
	});
	</script>
	<style>

body
{
	padding-bottom: 15px;
}
	table {
border-bottom: 3px solid #B2B2B2;
margin: 0px 0px 2em;
}
caption {
background: #EEE;
border: solid #B2B2B2;
border-width: 1px 0px 2px;
font: normal normal normal 1.1em/normal Georgia, Times, serif;
padding: 0.25em;
}

thead th {
text-align: left;
text-transform: uppercase;
}
th, td {
border: solid #CCC;
border-width: 0px 1px 1px;
padding: 0.25em 0.5em;
}
</style>

</head>
<body>
</body>
</html>